package pers.liyan.shi.springcloudkafkatest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import pers.liyan.shi.springcloudkafkatest.cache.RedisTest;
import pers.liyan.shi.springcloudkafkatest.domain.UserConvertor;
import pers.liyan.shi.springcloudkafkatest.domain.UserDTO;
import pers.liyan.shi.springcloudkafkatest.domain.UserVO;
import pers.liyan.shi.springcloudkafkatest.kafka.ProducerTest;

import java.util.concurrent.atomic.AtomicLong;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private ProducerTest producer;

    @Autowired
    private RedisTest redis;

    private AtomicLong id = new AtomicLong(0);

    @PostMapping("/send")
    public UserVO send(UserDTO user) {
        user.setId(id.getAndIncrement());
        producer.sendUserMsg(user);
        return UserConvertor.toUserVO(user);
    }

    @PostMapping("/set-redis")
    public UserVO setRedis(UserDTO user) {
        user.setId(id.getAndIncrement());
        redis.setTest(user);
        return UserConvertor.toUserVO(user);
    }
}
